package io.renren.modules.workmanagement.controller;

import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.workmanagement.constans.UserConstans;
import io.renren.modules.workmanagement.entity.ClassCourseEntity;
import io.renren.modules.workmanagement.service.ClassCourseService;
import io.renren.modules.workmanagement.vo.ClassCourse.ClassCourseSelectionVO;
import io.renren.modules.workmanagement.vo.SubmitWork;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * @author yewei
 * @email sunlightcs@gmail.com
 * @date 2022-04-16 16:11:24
 */
@RestController
@RequestMapping("workmanagement/classcourse")
public class ClassCourseController extends AbstractController {
    @Autowired
    private ClassCourseService classCourseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("department:sys:list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put(UserConstans.USER_ID_KEY, userId);
        PageUtils page = classCourseService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getMyClassList")
    @RequiresPermissions("teacher:class:list")
    public R getMyClassList(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        params.put(UserConstans.USER_ID_KEY, userId);
        PageUtils page = classCourseService.queryPageMyClass(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("department:sys:info")
    public R info(@PathVariable("id") Integer id) {
        ClassCourseEntity classCourse = classCourseService.getById(id);

        return R.ok().put("classCourse", classCourse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("department:sys:save")
    public R save(@RequestBody ClassCourseEntity classCourse) {
        Long userId = getUserId();
        classCourseService.saveClassCourse(classCourse, userId);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("department:sys:update")
    public R update(@RequestBody ClassCourseEntity classCourse) {
        try {
            classCourseService.updateById(classCourse);
        } catch (Exception e) {
            throw new RRException("该班级课程已存在");
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("department:sys:delete")
    public R delete(@RequestBody Integer[] ids) {
        classCourseService.removeClassCourseByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getSelecttions")
    public R getSelecttions() {
        Long userId = getUserId();
        ClassCourseSelectionVO vo = classCourseService.getSelections(userId);
        return R.ok().put("classCourse", vo);
    }

    @PostMapping("/submitWork")
    public R submitWork(@RequestBody SubmitWork submitWork) {
        classCourseService.submitWork(submitWork);
        return R.ok();
    }

}
